package com.runshu.newsmartark.base;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.runshu.newsmartark.event.BindEventBus;
import com.runshu.newsmartark.event.EventBusUtils;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2016/8/3.
 */
public abstract class BaseFragment extends Fragment {
    protected Context context;
    protected String tagName;
    private Unbinder mButterBind;

    public int mPage = 1;


    public View emptyView;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        tagName = this.getClass().getSimpleName();
        View layoutInflateView = getLayoutInflateView(inflater, container, savedInstanceState);
        mButterBind = ButterKnife.bind(this, layoutInflateView);
        // 若使用BindEventBus注解，则绑定EventBus
        if (this.getClass().isAnnotationPresent(BindEventBus.class)) {
            EventBusUtils.register(this);
        }
        initView(layoutInflateView);
        initData();
        bindListener();

        return layoutInflateView;
    }


    @Override
    public void onAttach(Context context) {
        this.context = context;
        super.onAttach(context);
    }

    /**
     * 初始化fragment
     *
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    protected abstract View getLayoutInflateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);

    /**
     * 用于findviewbyID
     *
     * @param layoutInflateView 当前加载的view
     */
    protected abstract void initView(View layoutInflateView);

    /**
     * 绑定数据
     */
    protected abstract void initData();

    /**
     * 绑定点击事件
     */
    protected abstract void bindListener();

    //    @Override
    public void onPause() {
        Glide.with(this).pauseRequests();
        super.onPause();
    }

    //    @Override
    public void onResume() {
        Glide.with(this).resumeRequests();
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mButterBind != null) {
            mButterBind.unbind();
        }
        // 若使用BindEventBus注解，则解绑定EventBus
        if (this.getClass().isAnnotationPresent(BindEventBus.class)) {
            EventBusUtils.unregister(this);
        }
    }
}
